﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ChuangDu_Spirit_QGB_BLL;
using ChuangDu_Spirit_QGB_BLL.FYBLL;
using ChuangDu_Spirit_QGB_BLLContainer;
using ChuangDu_Spirit_QGB_IBLL;
using ChuangDu_Spirit_QGB_Model;
using ChuangDu_Spirit_QGB_IBLL.IFYBLL;
using Webdiyer.WebControls.Mvc;
using System.IO;
using System.Data;
using Newtonsoft.Json;


namespace ChuangDu_Spirit_QGB.Areas.Sprintfuyu.Controllers
{
    public class SellController : Controller
    {
        #region//合同/订单管理区域ContractAndOrderInfo/OrderManagementShow
        // GET: Sprintfuyu/Sell

        IContractAndOrderInfoBLL ContractAndOrder = BLLContainer.Resolve<ContractAndOrderInfoBLL, IContractAndOrderInfoBLL>(); //从IOC容器中获取ContractAndOrderInfoBLL的实例
        public ActionResult OrderManagementShow(int pageid = 1, int pagesize = 10)//订单管理的显示
        {
            IEnumerable<ContractAndOrderInfo> list = ContractAndOrder.show();
            return View(list.ToPagedList(pageid, pagesize));
        }
        public ActionResult OrderMangementAdd()//订单管理的添加
        {
            return View();
        }
        [HttpPost]
        public ActionResult OrderMangement(ContractAndOrderInfo m)//订单管理的添加
        {
            int i = ContractAndOrder.Add(m);
            if (i > 0)
            {
                ViewBag.alert = 1;
            }
            else
            {
                ViewBag.alert = 0;
            }
            return View();
        }
        [HttpPost]
        public ActionResult OrderMangementAdd(ContractAndOrderInfo m)//订单管理的添加
        {
            int i = ContractAndOrder.Add(m);
            if (i > 0)
            {
                ViewBag.alert = "<script type='text/javascript'>layer.msg('添加成功')</ script > ";
            }
            else
            {
                ViewBag.alert = "<script type='text/javascript'>layer.msg('添加成功')</ script > ";
            }
            return View();



        }
        public ActionResult ssss()
        {
            return View();
        }

        [HttpPost]
        public JsonResult OrderMangementToALL()//订单管理的全部导出
        {
            var ids = Request["ids"];//选择序列号
            var name = Request["name"];//选择字段
            var nameall = Request["nameall"]; //全部字段（暂时没用）
            string[] ids1 = ids.Split(',');
            string[] name1 = name.Split(',');
            string[] nameal1 = nameall.Split(',');
            IEnumerable<ContractAndOrderInfo> IEnum = ContractAndOrder.show();
            DataTable list = JsonConvert.DeserializeObject<DataTable>(JsonConvert.SerializeObject(IEnum));

            if (list.Rows.Count > 0)
            {
                //创建一个新的excel工作簿
                NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
                //HSSFWorkbook book = new HSSFWorkbook();
                //再excel工作簿中创建一个新的sheet工作表

                NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("sheet1");


                //在工作表中添加一行
                NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0);
                for (var i = 0; i < name1.Length; i++)
                { //再工作表中创建标题列
                    row1.CreateCell(i).SetCellValue(name1[i].ToString());

                }

                for (int i = 0; i < list.Rows.Count; i++)
                {   //在工作表中添加一行
                    NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + 1);
                    for (var m = 0; m < ids.Length - 1; m++)
                    {    //再行中创建数据列
                        rowtemp.CreateCell(m).SetCellValue((list.Rows[i][Convert.ToInt32(ids1[m])]).ToString());
                    }

                }
                //我们使用NOPI创建一个NPOI.HSSF.UserModel.HSSFWorkbook book  创建一个book对象,将所有要处理的数据写入book对象中去，此时我们要配合缓存的使用，将book对象存在缓存中去,我们new一个Guid的随机缓存键值。
                var guid = "";
                guid = Guid.NewGuid().ToString();
                CacheHelper.SetCache(guid, book);
                return Json(guid, JsonRequestBehavior.AllowGet);
            }
            else
            {
                string ms = "1";
                return Json(ms);
            }
        }
        public ActionResult OrderMangementToExecl()//订单管理导入页面显示
        {
            return View();
        }
        [HttpPost]
        //暂时不对
        public ActionResult OrderMangementToExecl(HttpPostedFileBase execl, string ids)//订单管理导入页面
        {
            string path = Server.MapPath("ExcelFile");
            if (!Directory.Exists(path)) //判断目录下是否存在ExcelFile文件，不存在创建
            {
                Directory.CreateDirectory(path);
            }
            string afile = Path.GetFileName(execl.FileName); //获取导入文件名
                                                             //string alastname = afile.Substring(afile.LastIndexOf(".") + 1, (afile.Length - afile.LastIndexOf(".") - 1));//扩展名
                                                             //string file = Server.MapPath("/FILES/" + Guid.NewGuid() + "." + alastname);//保存路径
            string savepath = path + afile;
            if (execl.FileName.ToLower().IndexOf(".xls") > -1 || execl.FileName.ToLower().IndexOf(".xlsx") > -1)
            {
                execl.SaveAs(savepath);
                ExcelHelper.ExcelHelper execlhelper = new ExcelHelper.ExcelHelper(savepath);
                DataTable ta = execlhelper.ExcelToDataTable("", true);
                for (var i = 1; i < ta.Rows.Count; i++)
                {
                    ContractAndOrderInfo M = new ContractAndOrderInfo();

                    M.ID = Convert.ToInt32(ta.Rows[i][0]);
                    M.CAOOrganization = ta.Rows[i][1].ToString();
                    M.CAOSection = ta.Rows[i][2].ToString();
                    M.CAOprovince = ta.Rows[i][3].ToString();
                    M.CAOCity = ta.Rows[i][4].ToString();
                    M.CAOcounty = ta.Rows[i][5].ToString();
                    M.CAOFlowtype = ta.Rows[i][6].ToString();
                    M.CAOClientName = ta.Rows[i][7].ToString();
                    M.CAOFlownum = ta.Rows[i][8].ToString();
                    M.CAOFlowStep = ta.Rows[i][9].ToString();
                    M.CAOSellLeads = ta.Rows[i][10].ToString();
                    M.CAOItemName = ta.Rows[i][11].ToString();
                    M.CAONum = ta.Rows[i][12].ToString();
                    M.CAOType = ta.Rows[i][13].ToString();
                    M.CAOStatu = ta.Rows[i][14].ToString();
                    M.CAOstartTime = ta.Rows[i][15].ToString();
                    M.CAOendTime = ta.Rows[i][16].ToString();
                    M.CAOsumMoney = Convert.ToDecimal(ta.Rows[i][17]);
                    M.CAOSumMoneyBig = ta.Rows[i][18].ToString();
                    M.CAOMoneyOuter = ta.Rows[i][19].ToString();
                    M.CAOpaymentType = ta.Rows[i][20].ToString();
                    M.CAOClent = ta.Rows[i][21].ToString();
                    M.CAOMainName = ta.Rows[i][22].ToString();
                    M.CAOcreateTime = ta.Rows[i][23].ToString();
                    M.CAOMainclause = ta.Rows[i][24].ToString();
                    M.CAOremark = ta.Rows[i][25].ToString();
                    M.CAOappend = ta.Rows[i][26].ToString();
                    M.CAOPerson = ta.Rows[i][27].ToString();
                    M.CAOcreteaTime = ta.Rows[i][28].ToString();
                    M.CAOupdatePerson = ta.Rows[i][29].ToString();
                    M.CAOUpdateTiem = ta.Rows[i][30].ToString();
                    M.CAOisorNO = ta.Rows[i][31].ToString();
                    M.CAOENDtime1 = ta.Rows[i][32].ToString();
                    ContractAndOrder.Add(M);

                }
                return Content("<script>alter('导入成功')</script>");
            }
            else
            {
                return Content("<script>alter('请导入正确的格式')</script>");
            }

        }

        public FileStreamResult Download(string guid) //导出之后点击下载;
        {
            object obj = CacheHelper.GetCache(guid);
            NPOI.HSSF.UserModel.HSSFWorkbook book = obj as NPOI.HSSF.UserModel.HSSFWorkbook;
            if (book != null)
            {   //通过IO输出流进行文件的写入操作
                System.IO.MemoryStream ms = new System.IO.MemoryStream();

                book.Write(ms);//将缓存中的数据最终写入到excel工作表中

                ms.Seek(0, SeekOrigin.Begin);
                CacheHelper.RemoveAllCache(guid);
                return File(ms, "application/vnd.ms-excel", "信息表.xls");

            }
            else
            {
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                CacheHelper.RemoveAllCache(guid);
                return File(ms, "application/vnd.ms-excel", "信息表");
            }

        }
        ////导入导出原版实例
        //public ActionResult score(HttpPostedFileBase execl)
        //{
        //    string afile = execl.FileName;
        //    string alastname = afile.Substring(afile.LastIndexOf(".") + 1, (afile.Length - afile.LastIndexOf(".") - 1));//扩展名
        //    string file = Server.MapPath("/FILES/" + Guid.NewGuid() + "." + alastname);

        //    execl.SaveAs(file);
        //    ExcelHelper.ExcelHelper execlhelper = new ExcelHelper.ExcelHelper(file);
        //    DataTable ta = execlhelper.ExcelToDataTable("", false);
        //    tab1Entities4 db = new tab1Entities4(); //使用的EF ado.页一样
        //    for (var i = 1; i < ta.Rows.Count; i++)
        //    {
        //        grade m = new grade();
        //        for (var j = 1; j < ta.Columns.Count; j = j + 2)
        //        {
        //            m.gname = ta.Rows[i][0].ToString();
        //            m.gtheory = ta.Rows[i][j].ToString();
        //            m.gchange = ta.Rows[i][j + 1].ToString();
        //            m.gtime = ta.Columns[j].ToString();
        //            db.grade.Add(m);
        //            db.SaveChanges();
        //        }
        //    }

        //    return View();

        //}
        //导出
        //public FileResult SetFilePut()
        //{

        //    List<leimodel> uselist = new leibll().Show();

        //    if (uselist.Count > 0)
        //    {
        //        //创建一个新的excel工作簿
        //        NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
        //        //HSSFWorkbook book = new HSSFWorkbook();
        //        //再excel工作簿中创建一个新的sheet工作表

        //        NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("sheet1");

        //        //再工作表中创建标题列

        //        NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0);

        //        row1.CreateCell(0).SetCellValue("编号");
        //        row1.CreateCell(1).SetCellValue("姓名");
        //        row1.CreateCell(2).SetCellValue("性别");
        //        row1.CreateCell(3).SetCellValue("年龄");


        //        for (int i = 0; i < uselist.Count; i++)
        //        {
        //            NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + 1);

        //            rowtemp.CreateCell(0).SetCellValue(uselist[i].id);
        //            rowtemp.CreateCell(1).SetCellValue(uselist[i].name);
        //            rowtemp.CreateCell(2).SetCellValue(uselist[i].sex);
        //            rowtemp.CreateCell(3).SetCellValue(uselist[i].age);
        //        }

        //        //通过IO输出流进行文件的写入操作

        //        System.IO.MemoryStream ms = new System.IO.MemoryStream();

        //        book.Write(ms);//将缓存中的数据最终写入到excel工作表中

        //        ms.Seek(0, SeekOrigin.Begin);

        //        return File(ms, "application/vnd.ms-excel", "学生信息表.xls");


        //    }
        //    else
        //    {
        //        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        //        return File(ms, "application/vnd.ms-excel", "无人员信息");
        //    }

        public int OrderMangementDelete()
        {
            var sum = 0;
            var ids = Request["ids"];
            string[] ids1 = ids.Split(',');
            ContractAndOrderInfo m = new ContractAndOrderInfo();
            foreach (var i in ids1)
            {
                m.ID = Convert.ToInt32(i);
                sum = sum + ContractAndOrder.Delete(m);
            }
            if (sum == 0)
            {
                return 0;
            }
            else
            {
                return sum = 1;
            }
        }
        #endregion
        #region //销售订单管理SalesOrderInfo
        ISalesOrderInfoBLL SalesOrder = BLLContainer.Resolve<SalesOrderInfoBLL, ISalesOrderInfoBLL>(); //从IOC容器中获取SalesOrderInfoBLL的实例
        public ActionResult SalesOrderInfoshow(int pageid = 1) //销售订单管理显示
        {
            IEnumerable<SalesOrderInfo> list = SalesOrder.show();

            return View(list.ToPagedList(pageid, 4));
        }
        #endregion
        #region//开票记录OpenTicketInfo
        IOpenTicketInfoBLL OpenTicket = BLLContainer.Resolve<OpenTicketInfoBLL, IOpenTicketInfoBLL>(); //从IOC容器中获取OpenTicketInfoBLL的实例
        public ActionResult OpenTicketInfo(int pageid = 1)//开票记录显示
        {
            IEnumerable<OpenTicketInfo> list = OpenTicket.show();
            return View(list.ToPagedList(pageid, 4));
        }
        #endregion
        #region//合同调整CompactAdjustInfo
        ICompactAdjustInfoBLL CompactAdjust = BLLContainer.Resolve<CompactAdjustInfoBLL, ICompactAdjustInfoBLL>(); //从IOC容器中获取CompactAdjustInfoBLL的实例
        public ActionResult CompactAdjustInfoshow(int pageid = 1)//合同调整显示
        {
            IEnumerable<CompactAdjustInfo> list = CompactAdjust.show();
            return View(list.ToPagedList(pageid, 4));
        }
        #endregion
        #region//回款计划ReturnMoneyinfo
        IReturnMoneyinfoBLL ReturnMoney = BLLContainer.Resolve<ReturnMoneyinfoBLL, IReturnMoneyinfoBLL>(); //从IOC容器中获取ReturnMoneyinfoBLL的实例
        public ActionResult ReturnMoneyinfoshow(int pageid = 1)//回款计划显示
        {
            IEnumerable<ReturnMoneyinfo> list = ReturnMoney.show();
            return View(list.ToPagedList(pageid, 4));
        }
        #endregion
        #region//退款记录RefundRecordinfo
        IRefundRecordinfoBLL RefundRecord = BLLContainer.Resolve<RefundRecordinfoBLL, IRefundRecordinfoBLL>(); //从IOC容器中获取 IRefundRecordinfoBLL的实例
        public ActionResult RefundRecordinfoshow(int pageid = 1)//退款记录显示
        {
            IEnumerable<RefundRecordinfo> list = RefundRecord.show();
            return View(list.ToPagedList(pageid, 4));
        }
        #endregion
        #region //回款记录ReturenRecordInfo
        IReturenRecordInfoBLL ReturenRecord = BLLContainer.Resolve<ReturenRecordInfoBLL, IReturenRecordInfoBLL>(); //从IOC容器中获取ReturenRecordInfoBLL 的实例
        public ActionResult ReturenRecordInfoshow(int pageid = 1)//回款记录显示
        {
            IEnumerable<ReturenRecordInfo> list = ReturenRecord.show();
            return View(list.ToPagedList(pageid, 4));
        }
        #endregion
        #region//账单管理记录BillRecordInfo
        IBillRecordInfoBLL BillRecord = BLLContainer.Resolve<BillRecordInfoBLL, IBillRecordInfoBLL>(); //从IOC容器中获取 BillRecordInfoBLL 的实例
        public ActionResult BillRecordInfoshow(int pageid = 1)//账单管理记录显示
        {
            IEnumerable<BillRecordInfo> list = BillRecord.show();
            return View(list.ToPagedList(pageid, 4));
        }

        #endregion
        #region//费用报销ExpenseReimbursementInfo
        IExpenseReimbursementInfoBLL ExpenseReimbursement = BLLContainer.Resolve<ExpenseReimbursementInfoBLL, IExpenseReimbursementInfoBLL>(); //从IOC容器中获取ExpenseReimbursementInfoBLL 的实例
        public ActionResult ExpenseReimbursementInfoshow(int pageid = 1)//费用报销显示
        {
            IEnumerable<ExpenseReimbursementInfo> list = ExpenseReimbursement.show();
            return View(list.ToPagedList(pageid, 4));
        }
        #endregion
        #region//联系人管理ContactManagementInfo

        IContactManagementInfoBLL ContactManagement = BLLContainer.Resolve<ContactManagementInfoBLL, IContactManagementInfoBLL>(); //从IOC容器中获取ContactManagementInfoBLL 的实例
        public ActionResult ContactManagementInfoshow(int pageid = 1)//联系人管理
        {
            IEnumerable<ContactManagementInfo> list = ContactManagement.show();
            return View(list.ToPagedList(pageid, 4));
        }
        public int ContactManagementInfoDelete()
        {
            var sum = 0;
            var ids = Request["ids"];
            string[] ids1 = ids.Split(',');
            ContactManagementInfo m = new ContactManagementInfo();
            foreach (var i in ids1)
            {
                m.ID = Convert.ToInt32(i);
                sum = sum + ContactManagement.Delete(m);
            }
            if (sum == 0)
            {
                return 0;
            }
            else
            {
                return sum = 1;
            }
        }
        #endregion

        #region//记事本管理NotepadManagementInfo

        INotepadManagementInfoBLL NotepadManagement = BLLContainer.Resolve<NotepadManagementInfoBLL, INotepadManagementInfoBLL>(); //从IOC容器中获取NotepadManagementInfo 的实例
        public ActionResult NotepadManagementInfoshow(int pageid = 1)//记事本管理
        {
            IEnumerable<NotepadManagementInfo> list = NotepadManagement.show();
            return View(list.ToPagedList(pageid, 4));
        }
        public int NotepadManagementInfoDelete()
        {
            var sum = 0;
            var ids = Request["ids"];
            string[] ids1 = ids.Split(',');
            NotepadManagementInfo m = new NotepadManagementInfo();
            foreach (var i in ids1)
            {
                m.ID = Convert.ToInt32(i);
                sum = sum + NotepadManagement.Delete(m);
            }
            if (sum == 0)
            {
                return 0;
            }
            else
            {
                return sum = 1;
            }
        }
        #endregion
        #region//会员管理VIPInfo

        IVIPInfoBLL VIP = BLLContainer.Resolve<VIPInfoBLL, IVIPInfoBLL>();
        //从IOC容器中获取NotepadManagementInfo 的实例
        public ActionResult VIPInfoshow(int pageid = 1)//会员管理
        {
            IEnumerable<VIPInfo> list = VIP.show();
            return View(list.ToPagedList(pageid, 4));
        }
        public int VIPInfoDelete()
        {
            var sum = 0;
            var ids = Request["ids"];
            string[] ids1 = ids.Split(',');
            VIPInfo m = new VIPInfo();
            foreach (var i in ids1)
            {
                m.ID = Convert.ToInt32(i);
                sum = sum + VIP.Delete(m);
            }
            if (sum == 0)
            {
                return 0;
            }
            else
            {
                return sum = 1;
            }
        }
        #endregion
    }
}